package com.liu.blindBox.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.blindBox.entity.vo.BlindBoxInfoVO;
import com.liu.blindBox.mapper.BlindBoxMapper;
import com.liu.blindBox.service.BlindBoxService;
import com.liu.common.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BlindBoxServiceImpl implements BlindBoxService {

    @Resource
    private BlindBoxMapper blindBoxMapper;

    @Override
    public List<BlindBoxInfoVO> queryBlindBoxInfo(String keyword, String blindBoxType) {
        return blindBoxMapper.queryBlindBoxInfo(keyword, blindBoxType);
    }

    @Override
    public PageInfo<BlindBoxInfoVO> queryBlindBoxInfoBySaleNum(String keyword, String pageNum, String pageSize) {
        String endTime = DateUtils.getDateFormat(new Date(), "yyyy-MM-dd HH:mm:ss");
        String startTime = DateUtils.getDateFormat(DateUtils.addDay(new Date(), -30), "yyyy-MM-dd HH:mm:ss");
        PageInfo<BlindBoxInfoVO> blindBoxInfoPageInfo = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize)).doSelectPageInfo(() -> {
            blindBoxMapper.queryBlindBoxInfoBySaleNum(keyword,startTime,endTime);
        });
        // 新建分页
        PageInfo<BlindBoxInfoVO> blindBoxInfoVOPageInfo = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(blindBoxInfoPageInfo, blindBoxInfoVOPageInfo);
        // 对查询的list进行下一步操作
        List<BlindBoxInfoVO> blindBoxInfoVOS = new ArrayList<>(blindBoxInfoVOPageInfo.getList());
        List<BlindBoxInfoVO> blindBoxInfoVOList = blindBoxInfoVOS.stream().sorted(Comparator.comparing(BlindBoxInfoVO::getAllSaleNum).reversed()).collect(Collectors.toList());
        blindBoxInfoVOPageInfo.setList(blindBoxInfoVOList);
        return blindBoxInfoVOPageInfo;
    }

    @Override
    public BlindBoxInfoVO queryBlindBoxInfoById(String blindBoxId) {
        return blindBoxMapper.queryBlindBoxInfoById(blindBoxId);
    }

    @Override
    public List<BlindBoxInfoVO> queryHotDollBlindBoxBySaleNum() {
        Date date = new Date();
        String dateFormat = DateUtils.getDateFormat(date, "yyyy-MM-dd HH:mm:ss");
        return null;
    }

}
